<script setup lang="ts">
import { useCancelOrder, useDeleteOrder, useShowPrescription } from '@/composable'
import { OrderType } from '@/enums'
import type { ConsultOrderItem } from '@/types/consult'
import ConsultMore from './ConsultMore.vue'

const props = defineProps<{
  item: ConsultOrderItem
}>()
// 已被封装
// const showPopover = ref(false)
// const actions = computed(() => [
//   { text: '查看处方', disabled: !props.item.prescriptionId },
//   { text: '删除订单' },
// ])
// const onselect = (action: { text: string }, i: number) => {
//   if (i === 1) {
//     deleteConsultOrder(props.item)
//   }
//   if (i === 0) {
//     showPrescription(props.item.prescriptionId)
//   }
// }

//取消订单
// const lodaing = ref(false)
// const cancelConsultOrder = async (item: ConsultOrderItem) => {
//   try {
//     lodaing.value = true
//     await cancelOrder(item.id)
//     item.status = OrderType.ConsultCancel
//     item.statusValue = '已取消'
//     showSuccessToast('取消成功')
//     // eslint-disable-next-line @typescript-eslint/no-unused-vars
//   } catch (error) {
//     showFailToast('取消失败')
//   } finally {
//     lodaing.value = false
//   }
// }
const { loading, cancelConsultOrder } = useCancelOrder()
//删除订单
// 列表效果需要变动，因此通过emit传递信息修改前端数据
const emit = defineEmits<{
  (e: 'on-delete', id: string): void
}>()
const { deleteLoading, deleteConsultOrder } = useDeleteOrder(() => {
  emit('on-delete', props.item.id)
})
//已被封装
// const emit = defineEmits<{
//   (e: 'on-delete', id: string): void
// }>()
// const deleteLoading = ref(false)
// const deleteConsultOrder = async (item: ConsultOrderItem) => {
//   try {
//     deleteLoading.value = true
//     await deleteOrder(item.id)
//     showSuccessToast('删除成功')
//     emit('on-delete', item.id)
//     // eslint-disable-next-line @typescript-eslint/no-unused-vars
//   } catch (error) {
//     showFailToast('删除失败')
//   } finally {
//     deleteLoading.value = false
//   }
// }
const { showPrescription } = useShowPrescription()
</script>

<template>
  <div class="consult-item">
    <div class="head van-hairline--bottom">
      <img class="img" src="@/assets/avatar-doctor.svg" />
      <p>{{ item.docInfo?.name || '暂无医生问诊' }}</p>
      <span
        :class="{
          orange: item.status === OrderType.ConsultPay,
          green: item.status === OrderType.ConsultChat,
        }"
        >{{ item.statusValue }}</span
      >
    </div>
    <div class="body" @click="$router.push(`/user/consult/${item.id}`)">
      <div class="body-row">
        <div class="body-label">病情描述</div>
        <div class="body-value">{{ item.illnessDesc }}</div>
      </div>
      <div class="body-row">
        <div class="body-label">价格</div>
        <div class="body-value">¥ {{ item.payment.toFixed(2) }}</div>
      </div>
      <div class="body-row">
        <div class="body-label">创建时间</div>
        <div class="body-value tip">{{ item.createTime }}</div>
      </div>
    </div>
    <!-- 待支付 -->
    <div class="foot" v-if="item.status === OrderType.ConsultPay">
      <van-button
        class="gray"
        plain
        size="small"
        round
        :loading="loading"
        @click="cancelConsultOrder(item)"
        >取消问诊</van-button
      >
      <van-button type="primary" plain size="small" round :to="`/user/consult/${item.id}`"
        >去支付</van-button
      >
    </div>
    <!-- 待接诊 -->
    <div class="foot" v-if="item.status === OrderType.ConsultWait">
      <van-button
        :loading="loading"
        @click="cancelConsultOrder(item)"
        class="gray"
        plain
        size="small"
        round
        >取消问诊</van-button
      >
      <van-button type="primary" plain size="small" round :to="`/room?orderId=${item.id}`"
        >继续沟通</van-button
      >
    </div>
    <!-- 咨询中 -->
    <div class="foot" v-if="item.status === OrderType.ConsultChat">
      <van-button
        class="gray"
        plain
        size="small"
        round
        v-if="item.prescriptionId"
        @click="showPrescription(item.prescriptionId)"
        >查看处方</van-button
      >
      <van-button type="primary" plain size="small" round :to="`/room?orderId=${item.id}`"
        >继续沟通</van-button
      >
    </div>
    <!-- 已完成 -->
    <div class="foot" v-if="item.status === OrderType.ConsultComplete">
      <!-- 更多组件 -->
      <ConsultMore
        :disabled="!item.prescriptionId"
        @on-preview="showPrescription(item.prescriptionId)"
        @on-delete="deleteConsultOrder(item)"
      ></ConsultMore>
      <van-button class="gray" plain size="small" round :to="`/room?orderId=${item.id}`"
        >问诊记录
      </van-button>
      <van-button
        type="primary"
        plain
        size="small"
        round
        v-if="item.evaluateId"
        :to="`/room?orderId=${item.id}`"
        >查看评价</van-button
      >
      <van-button type="primary" plain size="small" round v-else :to="`/room?orderId=${item.id}`"
        >写评价</van-button
      >
    </div>
    <!-- 已取消 -->
    <div class="foot" v-if="item.status === OrderType.ConsultCancel">
      <van-button
        class="gray"
        plain
        size="small"
        round
        :loading="deleteLoading"
        @click="deleteConsultOrder(item)"
        >删除订单</van-button
      >
      <van-button type="primary" plain size="small" round to="/">咨询其他医生</van-button>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.consult-item {
  border-radius: 4px;
  box-shadow: 0px 0px 22px 0px rgba(245, 245, 245, 0.1);
  background-color: #fff;
  margin-bottom: 10px;
  .head {
    display: flex;
    align-items: center;
    height: 50px;
    padding: 0 15px;
    .img {
      width: 20px;
      height: 20px;
    }
    > p {
      flex: 1;
      font-size: 15px;
      padding-left: 10px;
    }
    > span {
      color: var(--cp-tag);
      &.orange {
        color: #f2994a;
      }
      &.green {
        color: var(--cp-primary);
      }
    }
  }
  .body {
    padding: 15px 15px 8px 15px;
    .body-row {
      display: flex;
      margin-bottom: 7px;
    }
    .body-label {
      width: 62px;
      font-size: 13px;
      color: var(--cp-tip);
    }
    .body-value {
      width: 250px;
      &.tip {
        color: var(--cp-tip);
      }
    }
  }
  .foot {
    padding: 0 15px 15px 15px;
    display: flex;
    justify-content: flex-end;
    align-items: center;
    .van-button {
      margin-left: 10px;
      padding: 0 16px;
      &.gray {
        color: var(--cp-text3);
        background-color: var(--cp-bg);
      }
    }
  }
}
</style>
